﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LabMS.BLL;
using System.IO;
using System.Collections.Generic;

namespace LabMS.Web.LabPersonnel
{
    public partial class AddStaff : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("RYJBXXGL");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
                //绑定性别
                list = DictionaryDropdownlist("201");
                StaffSexDDL.DataSource = list;
                StaffSexDDL.DataBind();

                //绑定文化程度
                list = DictionaryDropdownlist("education");
                StaffEducationDDL.DataSource = list;
                StaffEducationDDL.DataBind();
                //绑定论文级别
                list = DictionaryDropdownlist("paperlevel");
                StaffPaperLevelDDL.DataSource = list;
                StaffPaperLevelDDL.DataBind();
                //绑定著作级别
                list = DictionaryDropdownlist("publicationslevel");
                StaffPublicationsLevelDDL.DataSource = list;
                StaffPublicationsLevelDDL.DataBind();
                //绑定任聘情况
                list = DictionaryDropdownlist("employ");
                StaffEmployDDL.DataSource = list;
                StaffEmployDDL.DataBind();
                //绑定主要工作
                list = DictionaryDropdownlist("work");
                StaffWorkDDL.DataSource = list;
                StaffWorkDDL.DataBind();
                //绑定人员类别
                list = DictionaryDropdownlist("parttimetype");
                StaffPartTimeDDL.DataSource = list;
                StaffPartTimeDDL.DataBind();
                //绑定人员类别等级
                list = DictionaryDropdownlist("stafflevel");
                StaffLevelDDL.DataSource = list;
                StaffLevelDDL.DataBind();
                //绑定专家类别
                //list = DictionaryDropdownlist("expertlevel");
                //StaffExpertLevel.DataSource = list;
                //StaffExpertLevel.DataBind();

                //绑定实验室
                LabDropdownlist();
            }
            StaffBirth.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this);");
            StaffBirth.Attributes.Add("readOnly", "-1");
            StaffGraduteTime.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this);");
            StaffGraduteTime.Attributes.Add("readOnly", "-1");
            StaffLabTime.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this);");
            StaffLabTime.Attributes.Add("readOnly", "-1");
            StaffTakeInJob.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this);");
            StaffTakeInJob.Attributes.Add("readOnly", "-1");
            StaffPositionTime.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this);");
            StaffPositionTime.Attributes.Add("readOnly", "-1");
            StaffTitle.Attributes.Add("readOnly", "-1");
            StaffSubject.Attributes.Add("readOnly", "-1");
            StaffDisciplines.Attributes.Add("readOnly", "-1");
        }

        //绑定所属实验室
        protected void LabDropdownlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> LabTemp = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            LabTemp = lab.GetModelList("Lab_ParentID is null");

            int count = ParLab.Count, count1 = SonLab.Count;
            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                int m = 0;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        LabTemp.Insert(i + m + 1, labModel);
                        m++;
                    }
                }
            }
            StaffLabDDL.DataSource = LabTemp;
            StaffLabDDL.DataBind();
            ListItem item = new ListItem("无", "0");
            StaffLabDDL.Items.Insert(0, item);
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass= '" + code + "'";
            Dictionary dic = new Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        protected void Submit_OnClick(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(StaffID.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "人员编号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(StaffName.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "姓名不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(StaffBirth.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "出生年月不能为空!");
                return;
            }
            if (int.Parse(StaffLabDDL.SelectedValue.Trim()) == 0)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "实验室不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(StaffSubject.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "所属学科不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(StaffTitle.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "职称不能为空!");
                return;
            }
            int temp = 0;
            if (!string.IsNullOrEmpty(StaffLabSeniority.Text.Trim()))
            {
                if (!int.TryParse(StaffLabSeniority.Text, out temp))
                {
                    LabMS.Common.JShelper.JSAlert(Page, "err", "实验室工龄必须为数字!");
                    return;
                }
            }
            if (!string.IsNullOrEmpty(StaffPaperNum.Text.Trim()))
            {
                if (!int.TryParse(StaffPaperNum.Text, out temp))
                {
                    LabMS.Common.JShelper.JSAlert(Page, "err", "论文数量必须为数字!");
                    return;
                }
            }
            if (!string.IsNullOrEmpty(StaffPublicationsNum.Text.Trim()))
            {
                if (!int.TryParse(StaffPublicationsNum.Text, out temp))
                {
                    LabMS.Common.JShelper.JSAlert(Page, "err", "著作数量必须为数字!");
                    return;
                }
            }
            HttpPostedFile upPhoto = ImageUpload.PostedFile;
            if (upPhoto.ContentLength > 2097152)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "上传图片大小不能超过2MB");
                return;
            }
            //if (string.IsNullOrEmpty(StaffExpertLevel.SelectedValue))
            //{
            //    LabMS.Common.JShelper.JSAlert(Page, "err", "专家类别不能为空!");
            //    return;
            //}
            #endregion
            LabMS.Model.Staff staff = new LabMS.Model.Staff();
            LabMS.BLL.Staff staffinfo = new LabMS.BLL.Staff();

            staff.Staff_Code = StaffID.Text.Trim();//人员编号
            staff.Staff_Award = StaffAward.Text.ToString().Trim();//成果奖励
            staff.Staff_Birth = Convert.ToDateTime(StaffBirth.Text.Trim());//出生日期
            staff.Staff_Disciplines = tbDesciplineCode.Text.Trim();//主讲课程
            staff.Staff_Education = StaffEducationDDL.SelectedValue.Trim();//文化程度
            staff.Staff_Employ = StaffEmployDDL.SelectedValue.Trim();//任聘情况
            //string expertLevelList = "";
            //for (int i = 0; i < StaffExpertLevel.Items.Count; i++ )
            //{
            //    if (StaffExpertLevel.Items[i].Selected == true)
            //    {
            //        expertLevelList += StaffExpertLevel.Items[i].Value.Trim() + ";";//专家类别
            //    }
            //} 
            //staff.Staff_ExpertLevel = expertLevelList.Substring(0, expertLevelList.Length-1);
            staff.Staff_ForeignLanguage = StaffForeignLanguage.Text.Trim();//一外语种
            staff.Staff_ForeignLevel = StaffForeignLevel.Text.Trim();//一外水平
            staff.Staff_GradInstution = StaffGradInstitution.Text.Trim();//毕业院校
            DateTime tempTime = new DateTime();
            if (DateTime.TryParse(StaffGraduteTime.Text.Trim(), out tempTime))
            {
                staff.Staff_GraduteTime = tempTime;//毕业时间
            }
            staff.Staff_Lab = int.Parse(StaffLabDDL.SelectedValue.Trim());//所属实验室

            int tempInt = new int();
            if (int.TryParse(StaffLabSeniority.Text.Trim(), out tempInt))
            {
                staff.Staff_LabSeniority = tempInt;//实验室工龄
            }
            if (DateTime.TryParse(StaffLabTime.Text.Trim(), out tempTime))
            {
                staff.Staff_LabTime = tempTime;//实验室工作时间
            }
            staff.Staff_Level = StaffLevelDDL.SelectedValue.Trim();//人员类别等级
            staff.Staff_Name = StaffName.Text.Trim();//姓名
            staff.Staff_PaperLevel = StaffPaperLevelDDL.SelectedValue.Trim();//论文级别
            if (int.TryParse(StaffPaperNum.Text.Trim(), out tempInt))
            {
                staff.Staff_PaperNum = tempInt;//论文数量
            }
            staff.Staff_PartTime = StaffPartTimeDDL.SelectedValue.Trim();//人员类别
            if (DateTime.TryParse(StaffPositionTime.Text.Trim(), out tempTime))
            {
                staff.Staff_PositionTime = tempTime;//评职时间
            }
            staff.Staff_Professional = StaffProfessional.Text.Trim();//专业方向
            staff.Staff_PublicationsLevel = StaffPublicationsLevelDDL.SelectedValue.Trim();//著作水平

            if (int.TryParse(StaffPublicationsNum.Text.Trim(), out tempInt))
            {
                staff.Staff_PublicationsNum = tempInt;//著作数量
            }
            staff.Staff_ResearchSub = StaffResearchSub.Text.Trim();//在研课题
            staff.Staff_Sex = StaffSexDDL.SelectedValue.Trim();//性别
            staff.Staff_Subject = tbSubjectCode.Text.Trim();//所属学科
            staff.Staff_SubjectLevel = StaffSubjectLevel.Text.Trim();//课题级别
            staff.Staff_Summary = StaffSummary.Text.Trim();//备注
            if (DateTime.TryParse(StaffTakeInJob.Text.Trim(), out tempTime))
            {
                staff.Staff_TakeInJob = tempTime;//参加工作时间
            }
            staff.Staff_Title = tbStaffTitleCode.Text.Trim();//职称
            staff.Staff_Work = StaffWorkDDL.SelectedValue.Trim();//主要工作
            staff.Staff_Workload = StaffWorkload.Text.Trim();//工作量

            //上传图片
            try
            {
                if (ImageUpload.HasFile)
                {
                    int upPhotoLength = upPhoto.ContentLength;
                    string PhotoContentType = upPhoto.ContentType;
                    byte[] PhotoArray = new byte[upPhotoLength];
                    Stream PhotoStream = upPhoto.InputStream;
                    PhotoStream.Read(PhotoArray, 0, upPhotoLength);

                    staff.Staff_Photo = PhotoArray;//相片
                    staff.Staff_PhotoType = PhotoContentType;//图片类型
                }
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "Picwrong", "程序错误:图片保存出错");
            }


            try
            {
                staffinfo.Add(staff);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "添加成功!", "AddStaff.aspx");
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "AddFailed", "添加失败!");
            }
        }
    }
}
